<?php
	use ampleharvest\cServers;
	use ampleharvest\cConfig;
	
	require_once (dirname(dirname(dirname(__FILE__)))) . '/config/cConfig.php';
	require_once (dirname(dirname(dirname(__FILE__)))) . '/config/cServers.php';

	$str_ServerIDKey = "serverId";
	$str_IDKey = "id";
	$obj_Error	= new stdClass();
	$obj_Error->m_arr_Errors = Array();
	$obj_Error->m_bool_Active = false;
	
	$webConfig = new \ampleharvest\cConfig();
	
	$str_Location = cServers::$C_ARR_SERVERS["default"];
	$str_ServerID = $_REQUEST[$str_ServerIDKey];
	if(isset($_REQUEST[$str_ServerIDKey]))
	{
		
		if(isset(cServers::$C_ARR_SERVERS[$str_ServerID]))
		{
			$str_Location = cServers::$C_ARR_SERVERS[$str_ServerID];
			unset($str_ServerID);
		}
		else 
		{
			add_Error($str_ServerID." is an unknown server id please check with the system administrator");
		}
	}
	else 
	{
		add_Error("You must pass a server ID in the '$str_ServerIDKey' variable of the query string.");
	}
	if(isset($_REQUEST[$str_IDKey]))
	{
		$str_Location = str_ireplace("{id}", $_REQUEST[$str_IDKey], $str_Location);
		
			unset($_REQUEST[$str_IDKey]);
	}
	// TODO : REMOVE THE "AH" folder below when we pull all the code out to the top level
	$str_Location = str_ireplace("{ampleharvest}", $webConfig->WWW_ROOT_HTDOCS_AMPLEHARVEST."/AH", $str_Location);
	
	if($obj_Error->m_bool_Active)
	{
		$str_Location .= "&error=".json_encode($obj_Error);
	}
	
	
	foreach($_REQUEST as $str_Key => $str_Data);
	{
		if(!isset($str_Data))
		{
			$str_Data= "";
		}
		if(!isset($str_Key))
		{
			$str_Key= "";
		}
		$str_Location .= "&".$str_Key."=".$str_Data;
	}
		
	header("Location: ".$str_Location);
	
	function add_Error($str_Msg)
	{
		global $obj_Error;
		$obj_Error->m_arr_Errors[] = $str_Msg;
		$obj_Error->m_bool_Active = true;
	}